package server

import "sync"

type CredentialProvider interface {
	CheckUsername(username string) (bool, error)
	GetCredential(username string) (password string, found bool, err error)
}

func NewInMemoryProvider() *InMemoryProvider {
	return &InMemoryProvider{
		userPool: sync.Map{},
	}
}

type InMemoryProvider struct {
	userPool sync.Map
}

func (m *InMemoryProvider) CheckUsername(username string) (found bool, err error) {
	_, ok := m.userPool.Load(username)
	return ok, nil
}

func (m *InMemoryProvider) GetCredential(username string) (password string, found bool, err error) {
	v, ok := m.userPool.Load(username)
	if !ok {
		return "", false, nil
	}
	return v.(string), true, nil
}

func (m *InMemoryProvider) AddUser(username, password string) {
	m.userPool.Store(username, password)
}

type Provider InMemoryProvider
